﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Sale_Operation;
using Sale_Common;

namespace SaleSystem.ComPage
{
    public partial class frm_SaleBillSubList : PageBase
    {
        #region PageEvent
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                this.Id = ValueHandler.GetIntNumberValue(Request["Id"]);
                bnClose.OnClientClick = ExtAspNet.ActiveWindow.GetHideReference();
                InitControl();
                GetAndBind();
            }
            else if (Request[LinkService.RefreshTag] == LinkService.RefreshString)
            {
                GetAndBind();
            }
        }
        public void bnSearch_Click(object sender, EventArgs e)
        {
            GetAndBind();
        }
        protected void gv_List_PageIndexChange(object sender, ExtAspNet.GridPageEventArgs e)
        {
            gv_List.PageIndex = e.NewPageIndex;
        }
        protected void gv_List_RowCommand(object sender, ExtAspNet.GridCommandEventArgs e)
        {
            if (e.CommandName.ToLower() == "edit")
            {
                if (Request["Type"] == "1")
                {
                    win_Content2.Hidden = false;
                    win_Content2.IFrameUrl = "../Business/frm_SaleBillEdit.aspx?Id=" + ValueHandler.GetIntNumberValue(gv_List.DataKeys[e.RowIndex][0]);
                    win_Content2.Title = "编辑-" + ValueHandler.GetStringValue(gv_List.DataKeys[e.RowIndex][1]);
                }
                else if (Request["Type"] == "2")
                {
                    win_Content2.Hidden = false;
                    win_Content2.IFrameUrl = "../Business/frm_BuyEdit.aspx?Id=" + ValueHandler.GetIntNumberValue(gv_List.DataKeys[e.RowIndex][0]);
                    win_Content2.Title = "编辑-" + ValueHandler.GetStringValue(gv_List.DataKeys[e.RowIndex][1]);
                }
                else if (Request["Type"] == "3")
                {
                    win_Content2.Hidden = false;
                    win_Content2.IFrameUrl = "../Business/frm_SaleBillEdit.aspx?Id=" + ValueHandler.GetIntNumberValue(gv_List.DataKeys[e.RowIndex][0]);
                    win_Content2.Title = "编辑-" + ValueHandler.GetStringValue(gv_List.DataKeys[e.RowIndex][1]);
                }
            }
            else if (e.CommandName.ToLower() == "ok")
            {
                   //TreeNodeCollection nodes 
                    string strText = ValueHandler.GetStringValue(gv_List.DataKeys[e.RowIndex][1]);
                    string strValue = ValueHandler.GetStringValue(gv_List.DataKeys[e.RowIndex][0]);

                    ExtAspNet.PageContext.RegisterStartupScript(ExtAspNet.ActiveWindow.GetWriteBackValueReference(strText, strValue) + ExtAspNet.ActiveWindow.GetHidePostBackReference("GetSaleBillDetail"));
            
            }
        }
        #endregion
        #region Method
        private void InitControl()
        {
            //销售单类型
            BaseDataOp BaseManager = new BaseDataOp();
            DataTable dtList1 = BaseManager.GetList("BData_Type=4");
            this.ddl_SBill_Type.DataSource = dtList1;
            this.ddl_SBill_Type.DataBind();
            this.ddl_SBill_Type.Items.Insert(0, new ExtAspNet.ListItem("-未选择-", "-1"));
            //经办人
            SysUsersOp userManager = new SysUsersOp();
            DataTable dtUser = userManager.GetList("");
            this.ddl_SBill_User.DataSource = dtUser;
            this.ddl_SBill_User.DataBind();
            this.ddl_SBill_User.Items.Insert(0, new ExtAspNet.ListItem("-未选择-", "-1"));

        }
        private void GetAndBind()
        {
            //获取销售单
            InitCondition();
            int iRecordCount = 0; 
            dtList = Manager.GetSaleBillDetail(strCondition, gv_List.PageIndex, ref iRecordCount);
            this.gv_List.DataSource = dtList;
            this.gv_List.DataBind();
            this.gv_List.RecordCount = iRecordCount;

        }
        /// <summary>
        /// 获取查询条件
        /// </summary>
        public override void InitCondition()
        {
            strCondition = "";
            if (this.tb_SBill_Code.Text.Trim() != "")
            {
                strCondition += "SBill_Code LIKE '%" + ValueHandler.GetStringValue(this.tb_SBill_Code.Text, ValueHandler.CharHandlerType.IsRepChar) + "%' AND ";
            }
            if (this.tb_SBill_Code2.Text.Trim() != "")
            {
                strCondition += "SBill_Code2 LIKE '%" + ValueHandler.GetStringValue(this.tb_SBill_Code2.Text, ValueHandler.CharHandlerType.IsRepChar) + "%' AND ";
            }
            if (this.tb_SBill_Cust_Id.Text.Trim() != "")
            {
                strCondition += " Cust_Name LIKE '%" + ValueHandler.GetStringValue(this.tb_SBill_Cust_Id.Text, ValueHandler.CharHandlerType.IsRepChar) + "%' AND ";
            }
            if (this.ddl_SBill_Type.SelectedValue.Trim() != "-1")
            {
                strCondition += " SBill_Type =" + this.ddl_SBill_Type.SelectedValue + " AND ";
            }
            if (this.tb_PInfo_Name.Text.Trim() != "")
            {
                strCondition += " PInfo_Name LIKE '%" + ValueHandler.GetStringValue(this.tb_PInfo_Name.Text, ValueHandler.CharHandlerType.IsRepChar) + "%' AND ";
            }
            if (this.tb_PInfo_CH.Text.Trim() != "")
            {
                strCondition += " PInfo_CH LIKE '%" + ValueHandler.GetStringValue(this.tb_PInfo_CH.Text, ValueHandler.CharHandlerType.IsRepChar) + "%' AND ";
            }
            if (this.ddl_SBill_User.SelectedValue != "-1")
            {
                strCondition += " SBill_User =" + this.ddl_SBill_User.SelectedValue + " AND ";

            }
            if (!string.IsNullOrEmpty(strCondition))
                strCondition = strCondition.Substring(0, strCondition.Length - 4);

        }
        #endregion
        #region Property
        BillListOp Manager = new BillListOp();
        #endregion
    }
}
